package com.xiaoyg.algorithm;

import java.util.List;

public class InvertTree {
    public static void main(String[] args) {

        TreeNode root  = new TreeNode(1);
        TreeNode root1  = new TreeNode(2);
        TreeNode root2  = new TreeNode(3);
        TreeNode root3  = new TreeNode(4);
        TreeNode root4  = new TreeNode(5);

        root.left = root1;
        root.right = root2;

        root1.left = root3;
        root1.right = root4;


        inorderTraversal(root1);
        System.out.println();
        TreeNode rt = invertTree(root1);
        inorderTraversal(rt);


    }

    public static TreeNode invertTree(TreeNode root){
        if (root == null) return root;

        invertTree(root.left);
        invertTree(root.right);
        TreeNode temp = root.right;
        root.right = root.left;
        root.left = temp;
        return root;
    }


    public static void inorderTraversal(TreeNode root) {
        if (root == null) return ;
        inorderTraversal(root.left);
        System.out.print(root.val);
        inorderTraversal(root.right);
    }

    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }

        @Override
        public String toString() {
            return "TreeNode{" +
                    "val=" + val +
                    ", left=" + left +
                    ", right=" + right +
                    '}';
        }
    }
}
